package ui.tp3.TP3Wicket;

import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;

import org.apache.wicket.markup.html.form.Form;

import org.apache.wicket.markup.html.form.TextField;

import org.apache.wicket.markup.html.panel.FeedbackPanel;


import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.model.CompoundPropertyModel;



import ui.tp3.modelo.Coordenada;

import ui.tp3.modelo.Tablero;


public class HomePage extends WebPage {
	private static final long serialVersionUID = 1L;
	private final FeedbackPanel feedbackPanel;
	private Tablero tablero;
	private Coordenada coordTabl;


    public HomePage(final PageParameters parameters) {
    	
    	feedbackPanel = new FeedbackPanel("feedback");
				add(feedbackPanel);
				feedbackPanel.setOutputMarkupId(true);
    	
    	this.coordTabl = new Coordenada();
  
    	final WebMarkupContainer contenedorCreacionMapa= new WebMarkupContainer("divFormularioMapa");
    	contenedorCreacionMapa.setOutputMarkupId(true);
    	  	 
    	Form<Coordenada> form = new Form<Coordenada>("formulario1",new CompoundPropertyModel<Coordenada>(this.coordTabl));
    	form.setOutputMarkupId(true);
    	
    
    	
    	this.agregarComponentesDeCreacioDelMapa(form);
    	
    	
    	contenedorCreacionMapa.add(form);
   
    	add(contenedorCreacionMapa);
       
    	
    	
    }
    
    void agregarComponentesDeCreacioDelMapa(Form form){
    	
    	final TextField<Integer> dimensionMapaX = new TextField<Integer>("x");
    	final TextField<Integer> dimensionMapaY = new TextField<Integer>("y");
    	form.add(dimensionMapaX);
    	form.add(dimensionMapaY);
    	
    	dimensionMapaX.add(new CreacionMapaValidator());
    	dimensionMapaY.add(new CreacionMapaValidator());
    	
    	form.add(new AjaxButton("crearMapa") {

			private static final long serialVersionUID = 1L;

			protected void onError(AjaxRequestTarget target, Form<?> arg1) {
					target.add(feedbackPanel);
			}

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				
				int cx = HomePage.this.coordTabl.getX();
				int cy = HomePage.this.coordTabl.getY();
				
				tablero = new Tablero(cx,cy);
				
				tablero.generarPersonajesYEsferasPorDefecto();
				
				
				AccionesSobreElMapaPage actPage = new AccionesSobreElMapaPage(HomePage.this.tablero);
				this.setResponsePage(actPage);
			}
        	
        });
    	
    	
    }

}
